using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

/// <summary>
/// Summary description for Log
/// </summary>
namespace SDT.Libraries.Utilities
{
    public class LogHelper
    {
        public string FilePath { set; get; }   
        public  LogHelper(string filepath)
        {
            FilePath = filepath;
        
        }
        public  void Write(string logMessage)
        {
            try
            {
                WriteLog(String.Format("{0}\\log_{1:dd.MM.yyyy}.txt", FilePath, DateTime.Now), logMessage);
            }
            catch(Exception ext)
            {
                WriteLog(String.Format("{0}\\log.txt", FilePath), ext.Message);
            }
        }

        public void WriteLog(string logMessage)
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("<Log Entry> : {0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine("<Message>   : " + logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }

        public void Flush() 
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        
        
        }
        public void AddEntry(string logMessage, int id)
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("{0} {1} {2}", id.ToString(), DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine(logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }

        public void AddEntry(string logMessage)
        {
            FileInfo oFileInfo = new FileInfo(FilePath);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(FilePath, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine(logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }
        public  void Write(string path, string logMessage)
        {
            try
            {
                WriteLog(String.Format("{0}\\log_{1:dd.MM.yyyy}.txt", path, DateTime.Now), logMessage);
            }
            catch (Exception ext)
            {
                WriteLog(String.Format("{0}\\log.txt", path), ext.Message);
            }
        }

        private  void WriteLog(string path, string logMessage)
        {
            FileInfo oFileInfo = new FileInfo(path);
            DirectoryInfo oDirInfo = new DirectoryInfo(oFileInfo.DirectoryName);

            if (!oDirInfo.Exists)
                oDirInfo.Create();

            using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, System.IO.FileShare.Write))
            {
                using (StreamWriter w = new StreamWriter(fs))
                {
                    w.WriteLine("<Log Entry> : {0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
                    w.WriteLine("<Message>   : " + logMessage);
                    w.WriteLine("--------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
                fs.Close();
            }
        }

     
      
    }
    
}